//Chat class
//TODO: check IE compability

Chat = function() {
    this.timeout = 5000;
    this.setStamp();
    this.startTimeout();
};
    
Chat.prototype.startTimeout = function() {
    //refreshes chat window each 5000 ms
    window.setTimeout('c.refresh()', this.timeout);
};

Chat.prototype.refresh = function () {
    //refresh function - sends a request and displays it's result
    var func = function(http_request) {
        if (http_request.readyState == 4) {
            if (http_request.responseText) 
                this.getwall().innerHTML += http_request.responseText;
            this.getwall().scrollTop += 1000;
        }
    }
    var line = 'last=' + this.lastid;
    Lib.makeRequest('action.php', func, line, "POST", this);
    this.setStamp();
    this.startTimeout();
};

Chat.prototype.setStamp = function() {
    //Sets an id of last displayed message
    var func = function(http_request) {
        this.lastid = http_request.responseText || 0;
    }
    Lib.makeRequest('action.php',  func, 'action=last', "POST", this);
};

Chat.prototype.send = function() {
    //Sends a line to server
    var line = 'line=' + /*document.getElementById('start').value +*/ 
      Lib.getElementById('line').value +
      // document.getElementById('tales').value +
      '&nick=' + Lib.getElementById('nick').value +
      '&action=new_reply';
    Lib.getElementById('line').value = '';
    Lib.makeRequest('action.php', function(){}, line, "POST", this);
}

Chat.prototype.clean = function () {
    //Cleans wall of text
    this.getwall().innerHTML = '';
    Lib.getElementById('line').focus();
    this.setStamp();
};

Chat.prototype.enter = function(e) {
    //Allows to use Enter instead of clicking Send
    var keynum;
    if(e.keyCode) { // IE
        keynum = e.keyCode
    }
    else 
        if(e.which) {// Netscape/Firefox/Opera
            keynum = e.which
    }
    if(keynum == 13) 
        this.send()
    if(keynum == 115)
        this.clean()
};

Chat.prototype.getwall = function() {
    //Returns wall object
    return Lib.getElementById('iframe');
};

Chat.prototype.nickLink = function(nick) {
    //Adds a nick to line
    var line = Lib.getElementById('line');
    line.value += nick + ": ";
    line.focus();
    return false;
};
